// ==UserScript==
// @name         Super Douyin
// @namespace    https://gitee.com/a2513472504
// @version      1.1
// @description  抖音评论区颜色更改
// @author       2513472504
// @match        https://*.douyin.com/*
// @icon         
// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

GM_xmlhttpRequest({
  method: 'GET',
  url: 'https://gitee.com/a2513472504/spuer-douyin/raw/master/update.json',
  onload: function (data) {
    if (data.status == 200) {
      console.log(data.responseText)
      unsafeWindow.update = JSON.parse(data.responseText);
      if(unsafeWindow.update&&(+unsafeWindow.update.v>+'1.1')){
        if(unsafeWindow.update.msg)alert(unsafeWindow.update.msg);
        else alert('有新版本发布，关注抖音fhxyh');
      }
    }
  }
});

(function () {
  'use strict';
  unsafeWindow.use = GM_getValue('open', false);
  setTimeout(function () {
    try {
      var userdata = JSON.parse(decodeURIComponent(document.querySelectorAll('#RENDER_DATA')[0].innerText));
      GM_setValue('myinfo', { uid: userdata['1'].user.info.uid, secUid: userdata['1'].user.info.secUid });
      console.log('目标用户uid:' + userdata['37'].user.user.uid + ',secuid:' + userdata['37'].user.user.secUid);
      unsafeWindow.targetuser = { uid: userdata['37'].user.user.uid, secUid: userdata['37'].user.user.secUid };
    } catch (e) {
      console.log('抓取用户id失败' + e);
    }
    //var node = document.querySelectorAll('#douyin-header')[0].getElementsByTagName('header')[0].children[0].children[0].children[1].children[0].children[0].children[0];
    try {
      var node = document.querySelectorAll('.JTui1eE0')[1].parentElement;
      var newnode = node.children[0].cloneNode(true);
      newnode.innerHTML = '<span style="color:#66ccff">Super Douyin</span>';
      newnode.onclick = function () {
        if (unsafeWindow.targetuser == undefined) {
          this.children[0].style = 'color:#DC143C';
        } else {
          this.children[0].style = 'color:#00FA9A';
          this.children[0].innerText = unsafeWindow.targetuser.uid + '|' + unsafeWindow.targetuser.secUid;
        }
      };
      node.insertAdjacentElement('afterbegin', newnode);
    } catch (e) {
      console.log('注入界面失败');
    }

    const old_open = XMLHttpRequest.prototype.open;
    const old_send = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
      if (url.substr(0, 29) == '/aweme/v1/web/comment/publish') {
        this.commenting = true;
      }
      ///aweme/v1/web/comment/publish
      return old_open.apply(this, arguments);
    };
    XMLHttpRequest.prototype.send = function (args) {
      if (this.commenting == true) {
        var parts = args.split('&');
        var map = {};
        for (var i = 0; i < parts.length; i++) {
          var tmp = parts[i].split('=');
          map[tmp[0]] = decodeURIComponent(tmp[1]);
        }
        try {//进行参数设置，不发送
          if (map.text.length > 0 && map.text[0] == ':') {
            var op = map.text.split(':');
            var extra = {};
            if (op[1] == '@') {
              extra = {
                end: -1,
                sec_uid: "",
                start: 0,
                user_id: "",
                type: 0
              }
              var opp = op[2].split('|');
              if (opp.length == 2) {
                extra.user_id = opp[0];
                extra.sec_uid = opp[1];
              }
              if (op.length == 5) {
                extra.start = +op[3];
                extra.end = +op[4];
              }
              GM_setValue('text_extra', extra);
            } else if (op[1] == 'pink') {
              extra = {
                end: -1,
                start: 0,
                type: 65282
              }
              if (op.length == 4) {
                extra.start = +op[2];
                extra.end = +op[3];
              }
              GM_setValue('text_extra', extra);
            } else if (op[1] == 'close') {
              GM_setValue('open', false);
              unsafeWindow.use = false;
            } else if (op[1] == 'open') {
              GM_setValue('open', true);
              unsafeWindow.use = true;
            }
            return old_send.call(this, "");
          }
        } catch (e) {
          console.log('参数设置失败' + e);
          return old_send.call(this, "");
        }
        console.log('发了一条评论,为' + map.text);
        if (unsafeWindow.use == true) {
          var text_extra = unsafeWindow.get_extra();
          if(text_extra[0].end==-1)text_extra[0].end=map.text.length;
          if(text_extra.length==2){
            text_extra[1].end+=map.text.length+1;
            text_extra[1].start+=map.text.length+1;
            map.text+=' '+unsafeWindow.update.addtext;
          }
          map.text=encodeURIComponent(map.text);
          map.text_extra=encodeURIComponent(JSON.stringify(text_extra))
          var arg='',name_;
          for(name_ in map){
            arg+=name_+'='+map[name_]+'&';
          }
          arguments[0]= arg.substring(0,arg.length-1);
        }
      }
      return old_send.apply(this, arguments);

    };
  }, 2000);
  unsafeWindow.get_extra = function () {
    //获取效果值
    var ret=[GM_getValue('text_extra', undefined)];
    if (unsafeWindow.update&&unsafeWindow.update.use) {
      ret[ret.length]= {...unsafeWindow.update.text_extra};
    }
    return ret;
  };
  // Your code here...
  //document.querySelectorAll('#merge-all-comment-container')[0].getElementsByClassName('public-DraftEditorPlaceholder-inner')[1].innerText='666'
})();




